/**
 * 
 */
package org.optimus.utils;

import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.optimus.beans.Usuario;



/**
 * @author Juan Carlos Roman Perez
 *
 */
public class FacesUtils {

	public static Object getSessionAttribute( String attribute){
		ExternalContext externalContext =  FacesContext.getCurrentInstance().getExternalContext();
		HttpSession session = (HttpSession) externalContext.getSession(false);
		Object o = null;
		if(session != null){
			o = session.getAttribute(attribute);
		}
		return o;
	}
	
	
	public static void setSessionAttribute(String attribute,Object value){
		ExternalContext  externalContext = FacesContext.getCurrentInstance().getExternalContext();
		HttpSession session = (HttpSession) externalContext.getSession(false);
		session.setAttribute(attribute, value);
	}
	
	public static void removeAttribute( String attribute){
		ExternalContext  externalContext = FacesContext.getCurrentInstance().getExternalContext();
		HttpSession session = (HttpSession) externalContext.getSession(false);
		session.removeAttribute(attribute);
	}
	
	public static Usuario getUsuarioLogueado(){
		return (Usuario) getSessionAttribute("usuario");
	}
	
	public static void setUsuarioLogueado( Usuario usuario){
		setSessionAttribute("usuario", usuario);
	}
	
	public static void removeUsuarioLogueado() {
		removeAttribute("usuario");
	}
	
	public static boolean existeUsuarioLogueado() {
		if (getUsuarioLogueado() != null) {
			return true;
		}else{
			return false;
		}
	}
	
	public static void facesMessages(String texto, int tipo){
		switch (tipo) {
		case 1:
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error :",texto));
			break;
		case 2:
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Fatal",texto));
			break;
		case 3:
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info",texto));
			break;
		case 4:
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Advertencia",texto));
			break;
		}
		
	}
}
